#pragma once
#include <iostream>
#include <string>
#include <functional>
#include "InetAddr.h"
#include <unistd.h>

using namespace std;

class Connection;
class Reacotr;
using func_t = function<void(Connection *)>;

class Connection
{
public:
    Connection(int sock)
        : _sock(sock)
    {
    }

    string AppendIn(const char *buffer)
    {
        _inbuffer += buffer;
        return _inbuffer;
    }
    string AppendOut(const char *buffer)
    {
        _outbuffer += buffer;
        return _outbuffer;
    }
    string &Inbuffer()
    {
        return _inbuffer;
    }
    string &Outbuffer()
    {
        return _outbuffer;
    }
    bool OutbufferEmpty()
    {
        return _outbuffer.empty();
    }
    void OutbufferErase(int n)
    {
        _outbuffer.erase(0, n);
    }
    int Sockfd()
    {
        return _sock;
    }
    void SetEvent(uint32_t events)
    {
        _events = events;
    }
    void SetFunc(func_t recver, func_t sender, func_t excepter)
    {
        _recver = recver;
        _sender = sender;
        _excepter = excepter;
    }
    void Close()
    {
        if(_sock >= 0)
        {
            close(_sock);
        }
    }
    void SetSelf(Reacotr *r)
    {
        _r = r;
    }
    
    uint32_t GetEvent()
    {
        return _events;
    }
    ~Connection()
    {
    }

private:
    int _sock;
    string _inbuffer;
    string _outbuffer;

    uint32_t _events;
    InetAddr _addr;

public:
    func_t _recver;
    func_t _sender;
    func_t _excepter;
    Reacotr *_r;
};